package demo02ClassObj;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 09 - 13
 * Time:15:47
 */

import java.util.Arrays;

/***
 * 交换两个不等长数组
 * */
public class Test03 {
    public static void swap(int[] arr1,int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        //先确定出两个数组中较短的那一个数组
        //int len = Math.min(len1,len2);
        int len = len1 > len2 ? len2 : len1;
        for (int i = 0; i < len; i++) {
            int tmp = arr1[i];
            arr1[i] = arr2[i];
            arr2[i] = tmp;
        }
    }

    public static void main(String[] args) {

        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = {6,7,8};
        System.out.println("交换之前：");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        swap(arr1,arr2);
        System.out.println("交换之后：");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }
}
